﻿@{
    var singlelineDataTypes = new string[] { "Text", "MultilineText", "ImageUrl" };
}
@if(ViewData.TemplateInfo.TemplateDepth > 1) {
    @: @ViewData.ModelMetadata.SimpleDisplayText
} else {
    int cols = 2;
    int curr = 0;
    int colspan = 1;
    var props = ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit && !ViewData.TemplateInfo.Visited(pm) && !pm.IsComplexType);
    int totalCols = props.Count();
    Html.ValidationSummary();
    @Html.Hidden("EntityID", ViewData["EntityID"]);
    foreach (var prop in props.Where(o => o.TemplateHint == "HiddenInput"))
    {
         @Html.Editor(prop.PropertyName, new { data = this.Model })
    }
    <table id="edit-table" class="editor-table" cellpadding="0" cellspacing="0" border="0">
        @foreach (var prop in props.Where(o => o.TemplateHint != "HiddenInput"))
        {
            colspan = 1;
            if (singlelineDataTypes.Contains(prop.DataTypeName))
            {
                colspan = 3;
                if (curr % cols == cols - 1 || curr == totalCols - 1)
                {
                    curr++;
                @:</tr>
                }
            }
            if (curr % cols == 0)
            {
                if (singlelineDataTypes.Contains(prop.DataTypeName))
                {
                    curr++;
                }
                @:<tr>
            }
                <td class="caption">
                    <div class="editor-label" style="text-align: right;">
                        @Html.Label(prop.PropertyName)
                        @if (prop.IsRequired)
                        {
                            <span class='marked'>*</span>
                        }
                    </div>
                </td>
                <td colspan="@colspan">
                    <div class="editor-field">
                        @Html.Editor(prop.PropertyName, new { data = this.Model })
                        @Html.ValidationMessage(prop.PropertyName)
                    </div>
                </td>
                   if (curr % cols == cols - 1 || curr == totalCols - 1)
                   {
            @:</tr>
            }
                   curr++;
        }
    </table>
}
<script type="text/javascript">
    $(function () {
        $('#edit-table tr').each(function () {
            if ($('td', this).length == 4)
                return;
            $('td', this).eq(1).attr('colspan', '3');
        });
        $('#edit-table td[colspan=3] input').css('width', '97%');
    });
</script>